//
//  XLStatusOriginalView.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/26.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLStatusOriginalView: UIView {
    
    // 记录原创微博底部约束
    var originalViewBottomCon: Constraint?

    // 增加一个属性 共外界赋值
    var statusViewModel: XLHomeViewModel?{
        didSet{
            // 头像
            iconImage.xl_setImage(withUrlString:             statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default_big")
            // 昵称
            iconNameLabel.text = statusViewModel?.statusModel?.user?.name
            // 用户等级
            starLevelImage.image = statusViewModel?.mbrankImage
            // 微博认证
            avatarImage.image = statusViewModel?.verifiedImage
            // 微博内容
            contentLabel.text = statusViewModel?.statusModel?.text
            // 消息来源
            sourceLabel.text = statusViewModel?.sourceStr
            // 时间
            issueTimeLabel.text = statusViewModel?.timeStr
        
            // 配图
//            pictureView.pictureModel = statusViewModel?.statusModel?.pic_urls
            // 卸载约束
            originalViewBottomCon?.uninstall()
            if (statusViewModel?.statusModel?.pic_urls?.count)! > 0 {
                
                pictureView.pictureModel = statusViewModel?.statusModel?.pic_urls
                pictureView.isHidden = false
                snp_updateConstraints(closure: { (make) in
                    self.originalViewBottomCon = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                layoutIfNeeded()
            }else{
                // 没有配图
                pictureView.isHidden = true
                snp_updateConstraints(closure: { (make) in
                    self.originalViewBottomCon = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
            }
            layoutIfNeeded()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MRAK: 创建视图
    func  setupUI(){
//        backgroundColor = XLRandomColor()
        // 01 添加控件
        addSubview(iconImage)
        addSubview(iconNameLabel)
        addSubview(issueTimeLabel)
        addSubview(sourceLabel)
        addSubview(starLevelImage)
        addSubview(avatarImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 02 设置约束
        iconImage.snp_makeConstraints { (make) in
            make.top.left.equalTo(self).offset(FONTSIZETEN)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        iconNameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage)
            make.left.equalTo(iconImage.snp_right).offset(FONTSIZETEN)
        }
        
        issueTimeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(iconImage)
            make.left.equalTo(iconImage.snp_right).offset(FONTSIZETEN)
        }
        
        sourceLabel.snp_makeConstraints { (make) in
            make.top.equalTo(issueTimeLabel)
            make.left.equalTo(issueTimeLabel.snp_right).offset(FONTSIZETEN)
        }
        
        starLevelImage.snp_makeConstraints { (make) in
            make.centerY.equalTo(iconNameLabel)
            make.left.equalTo(iconNameLabel.snp_right).offset(FONTSIZETEN)
        }
        
        avatarImage.snp_makeConstraints { (make) in
            make.centerY.equalTo(iconImage.snp_bottom)
            make.centerX.equalTo(iconImage.snp_right)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconImage.snp_bottom).offset(FONTSIZETEN)
            make.left.equalTo(iconImage)
//            make.right.equalTo(self).offset(-FONTSIZETEN)
//            make.bottom.equalTo(self).offset(-5)
        }
        
        pictureView.snp_makeConstraints { (make) in
//            make.size.equalTo(CGSize(width: 100, height: 100))
            make.top.equalTo(contentLabel.snp_bottom).offset(5)
            make.left.equalTo(contentLabel)
//            make.bottom.equalTo(self).offset(-5)
        }
        
        self.snp_makeConstraints { (make) in
            self.originalViewBottomCon = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        
    }
    
    // 懒加载控件
    // iconImage 头像
    lazy var iconImage: UIImageView = UIImageView(imgNamed: "avatar_default_small")
    // 名称label
    lazy var iconNameLabel: UILabel = UILabel(text: "老宋", fontSize: XLNormalFontSize, textColor: UIColor.blue)
    // 时间
    lazy var issueTimeLabel: UILabel = UILabel(text: "时间", fontSize: XLSmallFontSize, textColor: XLTHENECOLOR)
    // 消息来源
    lazy var sourceLabel: UILabel = UILabel(text: "中国新青年五四杂志社", fontSize: XLSmallFontSize, textColor: XLTHENECOLOR)
    // starlevel 星级图片
    lazy var starLevelImage: UIImageView = UIImageView(imgNamed: "common_icon_membership_level1")
    // 认证
    lazy var avatarImage: UIImageView = UIImageView(imgNamed: "avatar_vip")
    // 微博内容
    lazy var contentLabel: UILabel = UILabel(text: "", fontSize: XLNormalFontSize, textColor: UIColor.black, maxWidth: XLSCREENW - 20)
    // 懒加载配图
    lazy var pictureView: XLStatusPictureView = {
       
        let pictureV = XLStatusPictureView()
//        pictureV.backgroundColor = XLRandomColor()
        pictureV.backgroundColor = UIColor.white
        return pictureV
        
    }()
    
    
    
    
}
